<?php

namespace app\admin\controller\chat;

use app\admin\model\Chat;
use app\admin\model\ChatLog;
use app\admin\model\Staff;
use app\admin\model\UserPvLog;
use app\common\Common;
use app\sys\model\UrlSourceLog;
use app\sys\service\GateCli;
use think\Db;
use think\Exception;
use think\facade\Log;
use think\facade\Validate;
use think\Request;
use \app\admin\model\UserList as user;
use \app\admin\model\User as UserModel;


class UserList extends Common
{
    // 用户列表
    public function userList(Request $request)
    {
        $data = $request->param();
        $validate = Validate::make([
            "user_type|会员类型" => "require",
        ]);

        if (!$validate->check($data)) {
            return failedJson($validate->getError());
        }
        $search = $request->param("search", "");
        $userType = $request->param("user_type", 0);
        $page = $request->param("page", 0);
        $limit = $request->param("limit", 10);
        $orderBy = $request->param("order_by",0);

        $staffInfo = $request->_user;
        $list = user::userLists($staffInfo["id"], $userType, $search, $orderBy,$page, $limit);
        return successJson("success", $list);
    }

    // 客服消息
    public function staffNews(Request $request)
    {
        $data = $request->param();
        $staffInfo = $request->_user;

        $info = user::staffNews($staffInfo["id"]);
        if (!$info) {
            return failedJson("未找到改客服~");
        }
        return successJson("success", $info);
    }

    // 发送消息
    public function sendInfo(Request $request)
    {
        $data = $request->param();
        $validate = Validate::make([
            "to_user_no|用户编号" => "require",
            "chat_info|聊天消息" => "require",
            "store_id|店铺id" => "require",
        ]);
        if (!$validate->check($data)) {
            return failedJson($validate->getError());
        }
        $staffInfo = $request->_user;
        $userInfo = \app\admin\model\User::where("no", $data["to_user_no"])->find();
        if (!$userInfo) {
            return failedJson("该用户不存在~");
        }
        $res = user::toUser($userInfo["id"],$userInfo["no"], $data["chat_info"], $staffInfo, $data["store_id"]);
        if($res) return failedJson($res);
        return successJson("success", "");
    }


    // 获取聊天消息
    public function getInfo(Request $request)
    {
        $data = $request->param();
        $validate = Validate::make([
            "user_no|用户编号" => "require",
            "chat_id|聊天id" => "require",
        ]);

        $chatLogId = $request->param("chat_log_id","0");
        $page = $request->param("page", 1);
        $limit = $request->param("limit", 10);
        if (!$validate->check($data)) {
            return failedJson($validate->getError());
        }
        $staffInfo = $request->_user;

        $chatDate = Chat::where("user_no", $data["user_no"])->where("staff_id", $staffInfo["id"])->where("id", $data["chat_id"])->find();
        if (!$chatDate) {
            return failedJson("信息不存在~");
        }
        $list = user::getInfo($staffInfo["id"], $chatDate["user_id"], $data["chat_id"],$chatLogId, $page, $limit);
        return successJson("success", $list);
    }

    // 修改消息状态
    public function upInfoStatus(Request $request)
    {
        $data = $request->param();
        $validate = Validate::make([
            "user_no|用户no" => "require",
            "chat_id|聊天id" => "require",
        ]);
        if (!$validate->check($data)) {
            return failedJson($validate->getError());
        }
        $staffInfo = $request->_user;
        $userInfo = \app\admin\model\User::where("no", $data["user_no"])->find();
        if (!$userInfo) {
            return failedJson("用户不存在~");
        }
        $res = user::upStatus($data["chat_id"], $userInfo["id"], $staffInfo["id"],"staff");
        if ($res) {
            return failedJson($res);
        }
        // 发送信息
        $isMen = 0;
        if($userInfo["mobile"])$isMen = 1;
        $gate  = new GateCli();
        $gate->staffUpNew($userInfo["no"],$userInfo["id"],$data["chat_id"],$isMen);

        return successJson("success", "修改成功");
    }

    // 标星
    public function makeSign(Request $request)
    {
        $data = $request->param();
        $validate = Validate::make([
            "chat_id|聊天chat_id" => "require",
            "collect|状态" => "require",
            "staff_no|客服" => "require",
        ]);
        if (!$validate->check($data)) {
            return failedJson($validate->getError());
        }
        $res = Chat::makeSign($data["chat_id"], $data["staff_no"], $data["collect"]);
        if ($res) {
            return failedJson($validate->getError());
        }
        return successJson("success", "修改成功");
    }

    // 用户信息
    public function userInfo(Request $request)
    {
        $data = $request->param();
        $validate = Validate::make([
            "chat_id|聊天id" => "require",
            "user_no|用户编号" => "require"
        ]);

        if (!$validate->check($data)) {
            return failedJson($validate->getError());
        }
        $staffInfo = $request->_user;
        $res = UserModel::info($data["chat_id"], $data["user_no"], $staffInfo);
        return successJson("success", $res);
    }

    // 保存用户信息
    public function saveUserInfo(Request $request)
    {
        $data = $request->param();
        $validate = Validate::make([
            "user_no|用户编号" => "require",
            "user_mobile|用户手机" => "mobile",
            "name|用户名称" => "require",
        ]);

        if (!$validate->check($data)) {
            return failedJson($validate->getError());
        }
        $data["remark"] = $request->param("remark","");
        $data["sex"] = $request->param("sex","");
        $staffInfo = $request->_user;
        Db::startTrans();
        try{
//            $url = UrlSourceLog::getUrlIng($data["user_no"]);
            $resU = UserModel::saveInfo(
                $data["user_no"], $data["user_mobile"], $data["sex"], $data["name"],
                $data["remark"],0,"客服保存",$staffInfo["staff_no"]
            );
            if ($resU) return failedJson($resU);
            $resC = Chat::upIsValid($staffInfo["staff_no"],$data["user_no"]);
            if ($resC) return failedJson($resC);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return failedJson("异常".$e->getMessage());
        }
        return successJson("success","成功");
    }

    // 浏览记录
    public function pvList(Request $request)
    {
        $data = $request->param();
        $validate = Validate::make([
            "chat_id|聊天id" => "require",
            "user_no|用户编号" => "require"
        ]);
        if (!$validate->check($data)) {
            return failedJson($validate->getError());
        }
        $log = UserPvLog::logList($data["user_no"],$data["chat_id"]);
        return successJson("success",$log);
    }

    // 客户对接
    public function toStaff(Request $request)
    {
        $data = $request->param();
        $validate = Validate::make([
            "chat_id|聊天id" => "require",
            "to_staff_no|负责客服编号" => "require"
        ]);
        if (!$validate->check($data)) {
            return failedJson($validate->getError());
        }

        $staffInfo = $request->_user;

        if($data["to_staff_no"] == $staffInfo["staff_no"]) {
            return failedJson("自己不能转让给自己");
        }

        $res = Staff::toStaff($data["chat_id"],$data["to_staff_no"],$staffInfo["staff_no"],$staffInfo["device_id"]);
        if ($res) return failedJson($res);

        return successJson("success","成功");
    }

    //修改状态
    public function staffStatus(Request $request)
    {
        $status = $request->param("status");
        $staffInfo = $request->_user;
        $resStaff = Staff::where("staff_no",$staffInfo["staff_no"])->find();
        if($resStaff["status"] == $status){
            return failedJson("状态相同~");
        }
        $res = Staff::where("staff_no",$staffInfo["staff_no"])
            ->data(["status"=>$status])->update();
        if(!$res)return failedJson("添加失败");
        // 状态 1 空闲 2 忙碌 3 离开
        $in = "";
        $out = "";
        if($status == 1){
            $in = $staffInfo["staff_no"];
        }else{
            $out = $staffInfo["staff_no"];
        }
        // 列表进出
        staff::allocStaff($in,$out,"af");
        return successJson("success","成功");
    }

    // 对话
    public static function staffChatSet(Request $request)
    {
        $data = $request->param();
        $validate = Validate::make([
            "user_no|用户编号" => "require",
            "chat_id|对话id"=>"require",
        ]);

        if (!$validate->check($data)) {
            return failedJson($validate->getError());
        }
        $userInfo = \app\admin\model\User::where("no", $data["user_no"])->find();
        if (!$userInfo) {
            return failedJson("用户不存在~");
        }
        $staffInfo = $request->_user;
        $res = ChatLog::setTalkNoAct($userInfo["id"],$staffInfo["id"],$data["chat_id"]);
        if ($res) {
            return failedJson($res);
        }
        // 聊天提示
        // 发起提示语
        chat::TisToUser($userInfo["id"], $staffInfo["id"], 0, "客服结束");
        return successJson("success",null);
    }
}